package com.ygqh.baby.controller.mobile;

import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.databind.util.JSONPObject;
import com.ygqh.baby.ao.Message;
import com.ygqh.baby.ao.QueryInfo;
import com.ygqh.baby.ao.ReturnReasonType;
import com.ygqh.baby.handler.CalculateReturnPriceHandler;
import com.ygqh.baby.handler.ReturnPriceHandler;
import com.ygqh.baby.model.*;
import com.ygqh.baby.po.*;
import com.ygqh.baby.service.YgReturnOrderLogService;
import com.ygqh.baby.service.YgReturnOrderService;
import com.ygqh.baby.service.YgSkuService;
import com.ygqh.baby.service.impl.DocumentConstant;
import com.ygqh.baby.utils.JsonUtils;
import com.ygqh.baby.utils.SessionUtil;
import com.ygqh.baby.utils.ThreadPool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 退货相关
 *
 * @author guohao
 */
@Controller
@RequestMapping("/mobile/return")
public class YgReturnOrderController extends BaseController {

    @Autowired
    private DocumentConstant documentConstant;
    @Autowired
    private YgReturnOrderService ygReturnOrderService;
    @Autowired
    private YgReturnOrderLogService ygReturnOrderLogService;
    @Autowired
    private YgSkuService skuService;
    @Autowired
    private ThreadPool threadPool;
    @Autowired
    private ReturnPriceHandler returnPriceHandler;
    @Autowired
    private CalculateReturnPriceHandler calculateReturnPriceHandler;

    /**
     * 获取可退换货订单列表
     *
     * @param queryInfo
     * @param callback
     * @return
     */
    @RequestMapping(value = "findOrderListForReturn", method = RequestMethod.GET)
    @ResponseBody
    public JSONPObject findOrderListForReturn(QueryInfo queryInfo, String callback) {
        YgUser user = SessionUtil.getCurrentUser();

        List<YgOrderModel> list = ygReturnOrderService.findOrderListForReturn(queryInfo, user.getId());
        Message msg = Message.success(documentConstant.MESSAGE_SUCCESS, list);
        return new JSONPObject(callback, msg);

    }

    /**
     * 获取退换货订单列表
     *
     * @param queryInfo
     * @param callback
     * @return
     */
    @RequestMapping(value = "findReturnList", method = RequestMethod.GET)
    @ResponseBody
    public JSONPObject findReturnList(QueryInfo queryInfo, String callback) {
        YgUser user = SessionUtil.getCurrentUser();

        List<YgReturnOrderModel> list = ygReturnOrderService.findReturnList(queryInfo, user.getId());
        Message msg = Message.success(documentConstant.MESSAGE_SUCCESS, list);
        return new JSONPObject(callback, msg);

    }

    /**
     * 获取商品详细用于订单退换货
     *
     * @param orderId
     * @param callback
     * @return
     */
    @RequestMapping(value = "findOrderInfo4Retrun", method = RequestMethod.GET)
    @ResponseBody
    public JSONPObject findOrderInfo4Retrun(Long orderId, String callback) {
        YgUser user = SessionUtil.getCurrentUser();
        YgOrderInfoModel model = ygReturnOrderService.findOrderInfo4Retrun(orderId, user.getId());
        Message msg = Message.success(documentConstant.MESSAGE_SUCCESS, model);
        return new JSONPObject(callback, msg);

    }

    /**
     * 获取退换货进度
     *
     * @param queryInfo
     * @param callback
     * @return
     */
    @RequestMapping(value = "getReturnScheduleList", method = RequestMethod.GET)
    @ResponseBody
    public JSONPObject getReturnScheduleList(String returnCode, String callback) {
        List<YgReturnOrderLog> list = ygReturnOrderLogService.getReturnScheduleList(returnCode);
        Message msg = Message.success(documentConstant.MESSAGE_SUCCESS, list);
        return new JSONPObject(callback, msg);

    }

    /**
     * 获取退货金额
     */
    @Deprecated
    @RequestMapping(value = "getReturnPriceOld", method = RequestMethod.GET)
    @ResponseBody
    public JSONPObject getReturnPrices(String returnInfo, String callback) {
        YgReturnInfoModel infoModel = JsonUtils.toObject(returnInfo, YgReturnInfoModel.class);
        Message msg = ygReturnOrderService.getReturnPrice(infoModel);
        if (msg.getType().equals(Message.Type.error)) {
            return new JSONPObject(callback, msg);
        }
        return new JSONPObject(callback, msg);

    }

    /**
     * 获取退货金额
     */
    @RequestMapping(value = "getReturnPrice", method = RequestMethod.GET)
    @ResponseBody
    public JSONPObject getReturnPriceV2(String returnInfo, String callback) {
        YgReturnInfoModel infoModel = JsonUtils.toObject(returnInfo, YgReturnInfoModel.class);
        Message msg = returnPriceHandler.getReturnPrice(infoModel);
        if (msg.getType().equals(Message.Type.error)) {
            return new JSONPObject(callback, msg);
        }

        return new JSONPObject(callback, msg);

    }

    /**
     * 保存退货单
     */
    @RequestMapping(value = "saveReturnOrder", method = RequestMethod.GET)
    @ResponseBody
    public JSONPObject saveReturnOrder(String returnInfo, ReturnReasonType returnReasonType, String returnReason,
                                       String imgPaths, String callback) {
        YgUser user = SessionUtil.getCurrentUser();
        if (returnReasonType == null) {
            return new JSONPObject(callback, Message.error("请选择退换货原因。", null));
        }

        YgReturnInfoModel infoModel = JsonUtils.toObject(returnInfo, YgReturnInfoModel.class);
        Message msg = returnPriceHandler.getReturnPrice(infoModel);
        if (msg.getType().equals(Message.Type.error)) {
            return new JSONPObject(callback, msg);
        }
        YgReturnInfoModel info = (YgReturnInfoModel) msg.getExtra();
        Message message = ygReturnOrderService.saveReturnOrder(infoModel, returnReasonType, returnReason,
                imgPaths, info, user.getId());
        if (message.getType().equals(Message.Type.success)) {
            threadPool.getExecutorService().execute(() ->
                    ygReturnOrderService.autoAuditReturnOrder((String) message.getExtra(), "system",
                            "", true));
        }
        return new JSONPObject(callback, message);
    }

    /**
     * 获取退换货单详细
     *
     * @param queryInfo
     * @param callback
     * @return
     */
    @RequestMapping(value = "findReturnOrderInfo", method = RequestMethod.GET)
    @ResponseBody
    public JSONPObject findReturnOrderInfo(String returnCode, String callback) {
        YgUser user = SessionUtil.getCurrentUser();
        YgReturnOrderInfoModel orderInfo = ygReturnOrderService.findReturnOrderInfo(returnCode, user.getId());
        return new JSONPObject(callback, Message.success(orderInfo));
    }

    /**
     * 根据productId获取有库存的SKU列表
     *
     * @param productId
     * @param callback
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "selectWithStock")
    @ResponseBody
    public JSONPObject selectWithStock(@RequestParam Long productId, String sizeName, @RequestParam String callback) throws Exception {
        List<YgSku> skus = skuService.selectWithStock(productId, sizeName);
        return new JSONPObject(callback, Message.success(skus));
    }

    /**
     * 创建换货单
     *
     * @param exchangeOrder
     * @param callback
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "createExchangeOrder")
    @ResponseBody
    public JSONPObject createExchangeOrder(@RequestParam String exchangeOrder, @RequestParam String callback) throws Exception {
        YgExchangeOrder eOrder = JSON.parseObject(exchangeOrder, YgExchangeOrder.class);
        Message message = ygReturnOrderService.createExchangeOrder(eOrder, SessionUtil.getCurrentUser().getId());
        if (message.getType().equals(Message.Type.success)) {
            threadPool.getExecutorService().execute(() -> ygReturnOrderService.autoAuditReturnOrder((String) message.getExtra(), "system", "", true));
        }
        return new JSONPObject(callback, message);
    }

    /**
     * 取消换货单
     *
     * @param id
     * @param callback
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "cancelReturnOrder")
    @ResponseBody
    public JSONPObject cancelReturnOrder(@RequestParam Long id, @RequestParam String callback) throws Exception {
        Long userId = SessionUtil.getCurrentUser().getId();
        Message message = ygReturnOrderService.cancelReturnOrder(id, userId);
        return new JSONPObject(callback, message);
    }

    /**
     * 更新物流信息
     *
     * @param orderLogisticsModel
     * @param callback
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "updateLogistics")
    @ResponseBody
    public JSONPObject updateLogistics(@RequestParam String orderLogisticsModel, String callback) throws Exception {
        YgReturnOrderLogisticsModel orderLogistics = JSON.parseObject(orderLogisticsModel, YgReturnOrderLogisticsModel.class);
        Message message = ygReturnOrderService.updateLogistics(orderLogistics, SessionUtil.getCurrentUser().getId());
        return new JSONPObject(callback, message);
    }

    /**
     * 根据订单获取物流信息
     *
     * @param returnOrderId
     * @param callback
     * @return
     */
    @RequestMapping(value = "findReturnLogistics")
    @ResponseBody
    public JSONPObject findReturnLogistics(@RequestParam String returnOrderId, String callback) {
        YgReturnOrder ygReturnOrder = ygReturnOrderService.findDetailByReCode(returnOrderId);
        YgSupplier ygSupplier = ygReturnOrderService.findSupplierByReturnOrder(ygReturnOrder);
        return new JSONPObject(callback, Message.success(ygSupplier));

    }
}
